
package vip.xiaonuo.biz.modular.lancerGoodsTag.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.pojo.vo.KV;
import vip.xiaonuo.biz.modular.service.BizGoodsTagRefService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.lancerGoodsTag.entity.BizGoodsTag;
import vip.xiaonuo.biz.modular.lancerGoodsTag.mapper.BizGoodsTagMapper;
import vip.xiaonuo.biz.modular.lancerGoodsTag.param.BizGoodsTagAddParam;
import vip.xiaonuo.biz.modular.lancerGoodsTag.param.BizGoodsTagEditParam;
import vip.xiaonuo.biz.modular.lancerGoodsTag.param.BizGoodsTagIdParam;
import vip.xiaonuo.biz.modular.lancerGoodsTag.param.BizGoodsTagPageParam;
import vip.xiaonuo.biz.modular.lancerGoodsTag.service.BizGoodsTagService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品标签表Service接口实现类
 *
 * @author Lancer
 * @date  2024/03/13 15:55
 **/
@Service
public class BizGoodsTagServiceImpl extends ServiceImpl<BizGoodsTagMapper, BizGoodsTag> implements BizGoodsTagService {

    @Override
    public Page<BizGoodsTag> page(BizGoodsTagPageParam bizGoodsTagPageParam) {
        QueryWrapper<BizGoodsTag> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(bizGoodsTagPageParam.getTagName())) {
            queryWrapper.lambda().like(BizGoodsTag::getTagName, bizGoodsTagPageParam.getTagName());
        }
        if(ObjectUtil.isNotEmpty(bizGoodsTagPageParam.getCurrEnergy())) {
            queryWrapper.lambda().gt(BizGoodsTag::getCurrEnergy, bizGoodsTagPageParam.getCurrEnergy());
        }
        if(ObjectUtil.isAllNotEmpty(bizGoodsTagPageParam.getSortField(), bizGoodsTagPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(bizGoodsTagPageParam.getSortOrder());
            queryWrapper.orderBy(true, bizGoodsTagPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(bizGoodsTagPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(BizGoodsTag::getId);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(BizGoodsTagAddParam bizGoodsTagAddParam) {
        BizGoodsTag bizGoodsTag = BeanUtil.toBean(bizGoodsTagAddParam, BizGoodsTag.class);
        this.save(bizGoodsTag);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BizGoodsTagEditParam bizGoodsTagEditParam) {
        BizGoodsTag bizGoodsTag = this.queryEntity(bizGoodsTagEditParam.getId());
        BeanUtil.copyProperties(bizGoodsTagEditParam, bizGoodsTag);
        this.updateById(bizGoodsTag);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<BizGoodsTagIdParam> bizGoodsTagIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(bizGoodsTagIdParamList, BizGoodsTagIdParam::getId));
    }

    @Override
    public BizGoodsTag detail(BizGoodsTagIdParam bizGoodsTagIdParam) {
        return this.queryEntity(bizGoodsTagIdParam.getId());
    }

    @Override
    public BizGoodsTag queryEntity(String id) {
        BizGoodsTag bizGoodsTag = this.getById(id);
        if(ObjectUtil.isEmpty(bizGoodsTag)) {
            throw new CommonException("商品标签表不存在，id值为：{}", id);
        }
        return bizGoodsTag;
    }

    @Override
    public List<KV> allTag() {
        LambdaQueryWrapper<BizGoodsTag> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.select(BizGoodsTag::getTagName);

        List<BizGoodsTag> list = this.list(queryWrapper);
        if (ObjectUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        return list.stream().map(x->{
            KV kv = new KV();
            kv.setValue(x.getTagName());
            return kv;
        }).collect(Collectors.toList());
    }

    @Autowired
    BizGoodsTagRefService refService;

    @Override
    public List<BizGoodsTag> getTagByGoodsId(String goodsId){
        // 查询tagid
        List<String> tagIdListByGoodsId = refService.getTagIdListByGoodsId(goodsId);
        LambdaQueryWrapper<BizGoodsTag> queryWrapper=new LambdaQueryWrapper<>();
        if (ObjectUtil.isEmpty(tagIdListByGoodsId)){
            return new ArrayList<>();
        }
        queryWrapper.in(BizGoodsTag::getId,tagIdListByGoodsId);
        return this.list(queryWrapper);
    }

    @Override
    public String getTagIdByName(String s) {
        LambdaQueryWrapper<BizGoodsTag> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(BizGoodsTag::getTagName,s);
        BizGoodsTag byId = this.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(byId)){
            return null;
        }
        return byId.getId();
    }
}
